package com.user.chris.util;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer001 {

    public static void main(String[] args) throws IOException, TimeoutException {
        final String QUEUE = "queue_test";

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("47.107.106.28"); // 设置mq所在服务器的ip
        factory.setPort(5672);  // 设置mq所在服务器的端口号
        factory.setUsername("admin");// 设置登录用户名
        factory.setPassword("admin");// 设置登录密码

        // 创建与RabbitMQ服务的TCP连接
        Connection connection = factory.newConnection();
        // 创建信道
        Channel channel = connection.createChannel();
        // 在消费者这边设置不公平分发 默认是0轮训 1是不公平消费 2预取值 
        // channel.basicQos(1);
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            byte[] body = message.getBody();
            String s = new String(body);
            System.out.println(s);
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
        };
        // 参数1 消费那个队列
        // 参数2 消费成功是否自动应答 true自动应答
        // 参数3 消费成功回调函数
        // 参数4 失败回调
        channel.basicConsume(QUEUE, false, deliverCallback, consumerTag -> {
            System.out.println("-----失败回调");
        });
    }
}
